package uk.ac.ebi.bioinvindex.model.impl.processing;

import uk.ac.ebi.bioinvindex.model.impl.AnnotatableImpl;
import uk.ac.ebi.bioinvindex.model.impl.ProtocolImpl;
import uk.ac.ebi.bioinvindex.model.impl.term.Parameter;
import uk.ac.ebi.bioinvindex.model.impl.term.ParameterValue;

import javax.persistence.*;
import java.util.Collection;
import java.util.ArrayList;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Jan 2, 2008
 */
@Entity
@Table(name = "ProtocolApplication")
public class ProtocolApplication extends AnnotatableImpl{

	private int order;

	private ProtocolImpl protocol;

	private Collection<ParameterValue> parameterValues = new ArrayList<ParameterValue>();

	public ProtocolApplication() {
	}

	public ProtocolApplication(ProtocolImpl protocol) {
		this.protocol = protocol;
	}

	@ManyToOne
	public ProtocolImpl getProtocol() {
		return protocol;
	}

	protected void setProtocol(ProtocolImpl protocol) {
		this.protocol = protocol;
	}

	@OneToMany(targetEntity = uk.ac.ebi.bioinvindex.model.impl.term.ParameterValue.class,
			mappedBy = "protocolApplication", cascade = CascadeType.ALL)
	public Collection<ParameterValue> getParameterValues() {
		return parameterValues;
	}

	public void addParameterValue(ParameterValue value) {
		parameterValues.add(value);
		value.setProtocolApplication(this);
	}

	public boolean removeParameterValue(ParameterValue value) {
		return parameterValues.remove(value);
	}

	protected void setParameterValues(Collection<ParameterValue> parameterValues) {
		this.parameterValues = parameterValues;
	}

	@Column(name = "position" )
	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}


	public String toString () {
		StringBuilder result = new StringBuilder ( "ProtocolApplication { id =  " + this.getId() );
		ProtocolImpl protocol = this.getProtocol ();
		result.append ( " protocol = " + protocol.getName () );
	  result.append ( "  parameters: [ "  );
	  String separator = "";
	  for ( ParameterValue paramValue: this.getParameterValues () ) {
	  	result.append ( separator );
	  	Parameter paramType = paramValue.getType ();
	  	String paramName = paramType.getValue ();
	  	result.append ( paramName != null && paramName.length () != 0 ? paramName : "?" );
	  	result.append ( " = " + paramValue.getValue () );
	  	separator = ", ";
	  }
	  result.append ( "] }" );
		return result.toString ();
	}


}
